
package com.kugou.game.sdk.ui.fragment;

import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.base.BaseSDKWorkerFragment;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.FAQListItem;
import com.kugou.game.sdk.interfaces.OnLoadFAQListListener;
import com.kugou.game.sdk.ui.activity.CommonSdkActivity;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.Rx;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * 描述:FAQ页面列表
 *
 * @author jamywang
 * @since 2015年11月17日 下午2:56:38
 */
public class FAQListFragment extends BaseSDKWorkerFragment implements OnItemClickListener {

    private Activity context;

    private ArrayAdapter<String> adapter;

    private String[] titleData = null;

    private int[] titleId = null;

    /**
     * 消息相关指令
     */
    private static final int MSG_REQUEST_FAQ_LIST = 0;

    private static final int MSG_REQUEST_FAQ_LIST_SUCCESS = 1;

    private static final int MSG_REQUEST_FAQ_LIST_FAILE = 2;

    private static final int MSG_SHOW_LOADING_VIEW = 3;

    /**
     * 页面控件
     */
    private View layoutEmail;

    private ListView mListView;

    private TipsLayout mTipsLayout;

    private View footerView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = null;
        view = inflater.inflate(Rx.layout.kg_fragment_faq_list, null);
        if (context != null && context instanceof CommonSdkActivity) {
            ((BaseCommonTitleFragmentActivity) context).setFAQPageTitleStyle();
        }
        initView(view);
        sendEmptyBackgroundMessage(MSG_REQUEST_FAQ_LIST);
        return view;
    }

    public void initView(View view) {
        layoutEmail = view.findViewById(Rx.id.kg_layout_email);
        mListView = (ListView) view.findViewById(Rx.id.kg_listView);
        footerView = view.findViewById(Rx.layout.kg_layout_footer_faq);

        mTipsLayout = (TipsLayout) view.findViewById(Rx.id.kg_tips_layout);
        mTipsLayout.setLoadingText("");
        mTipsLayout.setLoadingViewBackground(Rx.drawable.kg_gamecenter_loading);
        mTipsLayout.setOnRefreshButtonClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                sendEmptyBackgroundMessage(MSG_REQUEST_FAQ_LIST);
            }
        });
    }

    private void showLoadingView() {
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_LOADING);
        layoutEmail.setVisibility(View.GONE);
    }

    private void showListView() {
        mTipsLayout.setVisibility(View.GONE);
        layoutEmail.setVisibility(View.VISIBLE);
    }

    private void showLayoutResult() {
        layoutEmail.setVisibility(View.GONE);
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_FAILE);
    }

    private void showLayoutEmpty() {
        layoutEmail.setVisibility(View.GONE);
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_EMPTY_CONTENT);
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        switch (msg.what) {
            case MSG_REQUEST_FAQ_LIST:
                sendEmptyUiMessage(MSG_SHOW_LOADING_VIEW);
                final Message message = new Message();
                UserManager.getInstance().requestFAQList(new OnLoadFAQListListener() {

                    @Override
                    public void onSuccess(ArrayList<FAQListItem> faqList) {
                        message.what = MSG_REQUEST_FAQ_LIST_SUCCESS;
                        message.obj = faqList;
                        sendUiMessage(message);
                    }

                    @Override
                    public void onFailed(String prompt) {
                        message.what = MSG_REQUEST_FAQ_LIST_FAILE;
                        message.obj = prompt;
                        sendUiMessage(message);
                    }
                });
                break;

            default:
                break;
        }
    }

    @Override
    public void handleUiMessage(Message msg) {
        switch (msg.what) {
            case MSG_SHOW_LOADING_VIEW:
                showLoadingView();
                break;
            case MSG_REQUEST_FAQ_LIST_SUCCESS:
                ArrayList<FAQListItem> faqList = (ArrayList<FAQListItem>) msg.obj;
                if (faqList.size() > 0) {
                    titleData = new String[faqList.size()];
                    titleId = new int[faqList.size()];
                    for (int i = 0; i < faqList.size(); i++) {
                        titleData[i] = faqList.get(i).getTitle();
                        titleId[i] = faqList.get(i).getItemId();
                    }

                    adapter = new ArrayAdapter<String>(context, Rx.layout.kg_item_faq_list,
                            titleData);
                    mListView.setAdapter(adapter);
                    mListView.setOnItemClickListener(this);
                    showListView();
                } else if (faqList.size() == 0) {
                    showLayoutEmpty();
                } else {
                    sendEmptyUiMessage(MSG_REQUEST_FAQ_LIST_FAILE);
                }
                break;
            case MSG_REQUEST_FAQ_LIST_FAILE:
                showLayoutResult();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
        context.startActivity(CommonSdkActivity.creatIntent(context,
                FAQDetailFragment.class.getName(), Rx.string.kg_custom_service_faqpage).putExtra(
                "id", titleId[position]));
    }

}
